package com.xh.lesson.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.xh.lesson.entity.SysFun;
import com.xh.lesson.entity.SysHouse;
import com.xh.lesson.entity.SysHouseFun;
import com.xh.lesson.exception.BusinessException;
import com.xh.lesson.exception.code.BaseResponseCode;
import com.xh.lesson.mapper.SysHouseFundMapper;
import com.xh.lesson.service.SysFunService;
import com.xh.lesson.service.SysHouseFunService;
import com.xh.lesson.service.SysHouseService;
import com.xh.lesson.utils.PageUtils;
import com.xh.lesson.vo.req.AllUpdateReqVo;
import com.xh.lesson.vo.req.HomeAddReqVO;
import com.xh.lesson.vo.req.HomeUpdateReqVo;
import com.xh.lesson.vo.resp.HomeInfo;
import com.xh.lesson.vo.resp.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 柴&许
 * @since 2021-07-01
 */
@Service
@Slf4j
public class SysHouseFunServiceImpl extends ServiceImpl<SysHouseFundMapper, SysHouseFun> implements SysHouseFunService {

    @Autowired
    private SysFunService funService;
    @Autowired
    private SysHouseService houseService;

    @Override

    public List<AllUpdateReqVo> selectHomeById(Integer id) {
        List<AllUpdateReqVo> list = baseMapper.selectHomeById(id);
        return list;
    }

    public PageVO<HomeInfo> selectAll(HomeInfo vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<HomeInfo> list = baseMapper.selectAll(vo);
        return PageUtils.getPageVO(list);
    }

    @Override
    public void UpdateHome(HomeUpdateReqVo vo) {
        SysHouseFun home = baseMapper.selectById(vo.getHomeid());
        if (home == null) {
            log.error("传入 的 id:{}不合法", vo.getHomeid());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        SysHouseFun update = new SysHouseFun();
        update.setHomeid(vo.getHomeid());
        update.setFunid(vo.getFunid());
        update.setHomename(vo.getHomename());
        update.setHomenum(vo.getHomenum());
        update.setHouserid(vo.getHouserid());

        SysFun fun = funService.selectFunById(vo.getFunid());
        if (fun == null) {
            log.error("传入 的 id:{}不合法", vo.getHomeid());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        SysHouse sysHouse = new SysHouse();
        sysHouse.setHouserid(vo.getHouserid());
        sysHouse.setArea(vo.getArea());
        sysHouse.setFloorspace(vo.getFloorspace());
        sysHouse.setHousename(vo.getHousename());
        sysHouse.setLevel(vo.getLevel());
        boolean b = houseService.updateById(sysHouse);
        if (!b) {
            log.error("传入 的 id:{}不合法", vo.getHomeid());
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }

        SysFun sysFun = new SysFun();
        sysFun.setFun(vo.getFun());
        sysFun.setFunuse(vo.getFunuse());
        sysFun.setEnterprise(vo.getEnterprise());
        sysFun.setForclass(vo.getForclass());
        sysFun.setDepart(vo.getDepart());
        sysFun.setDeploy(vo.getDeploy());
        sysFun.setDetailone(vo.getDetailone());
        sysFun.setDetailtwo(vo.getDetailtwo());
        sysFun.setDetailthree(vo.getDetailthree());
        sysFun.setRemark(vo.getRemark());
        sysFun.setStatus(vo.getStatus());

        UpdateWrapper<SysFun> wrapper = new UpdateWrapper<>();
        wrapper.eq("funid", vo.getFunid());

        boolean save = funService.update(sysFun, wrapper);
        if (!save) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
        int count = baseMapper.updateById(update);
        if (count != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }

    }

    @Override
    public SysHouseFun addHome(HomeAddReqVO vo) {
        SysFun sysFun = funService.addFun(vo);
        if (sysFun == null) {
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }
        SysHouseFun sysHouseFun = new SysHouseFun();
        BeanUtils.copyProperties(vo, sysHouseFun);
        sysHouseFun.setFunid(sysFun.getFunid());
        sysHouseFun.setHouserid(vo.getHouserid());
        int insert = baseMapper.insert(sysHouseFun);
        if (insert != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
        return sysHouseFun;
    }

    @Override
    public void deleteHouseFun(Integer homeid) {
        SysHouseFun sysHouseFun = baseMapper.selectById(homeid);
        if (sysHouseFun == null) {
            log.error("传入 的 id:{}不合法", homeid);
            throw new BusinessException(BaseResponseCode.DATA_ERROR);
        }

        Integer funid = sysHouseFun.getFunid();
        funService.deleteFun(funid);
        baseMapper.deleteById(homeid);


        QueryWrapper<SysHouseFun> wrapper = new QueryWrapper<>();
        wrapper.eq("houserid",sysHouseFun.getHouserid());
        List<SysHouseFun> sysHouseFuns = baseMapper.selectList(wrapper);
        if (sysHouseFuns.size() == 0) {
            houseService.deleted(sysHouseFun.getHouserid());
        }else{
            log.error("无法删除该房间", sysHouseFun.getHouserid());
            throw new BusinessException(BaseResponseCode.HOUSE_NOT_ALL_CHECK);
        }

    }

    @Override
    public void deleteHouseFun(List<Integer> homeIds) {
        for (Integer home : homeIds) {

            SysHouseFun sysHouseFun = baseMapper.selectById(home);

            if (sysHouseFun == null) {
                log.error("传入 的 id:{}不合法", home);
                throw new BusinessException(BaseResponseCode.DATA_ERROR);
            }

            funService.deleteFun(sysHouseFun.getFunid());


            int i = baseMapper.deleteById(home);
            if (i != 1) {
                throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
            }



            houseService.deleted(sysHouseFun.getHouserid());

        }


    }

    @Override
    public SysHouseFun soloAddHome(HomeAddReqVO vo) {
        SysHouseFun sysHouseFun = new SysHouseFun();
        BeanUtils.copyProperties(vo, sysHouseFun);
        sysHouseFun.setFunid(vo.getFunid());
        sysHouseFun.setHouserid(vo.getHouserid());
        int insert = baseMapper.insert(sysHouseFun);
        if (insert != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
        return sysHouseFun;
    }

}